package com.cz.basetool.ui_work.util.time;

import android.content.Context;
import android.graphics.Color;
import android.view.View;


import com.bigkoo.pickerview.TimePickerView;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by 陈章 on 2017/4/5 0005.
 * func: 时间选择器
 */

public class TimePicker {
    private Context mContext;
    private TimePickerView pvTime;
    private TimeCallBack timeCallBack;


    public TimePicker(Context mContext) {
        this.mContext = mContext;
    }

    public void setTimeCallBack(TimeCallBack timeCallBack) {
        this.timeCallBack = timeCallBack;
    }

    public void show(String title, Calendar startDate, Calendar endDate ){
        show(title,startDate,endDate,null,null);
    }

    public void show(String title,TimePickerView.Type type ){
        Calendar startTime = Calendar.getInstance();
        startTime.set(TimeUtil.getCurrentTimeDetail().getYear() + 1, 0, 30);
        Calendar endTime = Calendar.getInstance();
        endTime.set(TimeUtil.getCurrentTimeDetail().getYear() - 50 , 0, 30);
        this.show(title,startTime,endTime,type);
    }


    public void show(String title, Calendar startDate, Calendar endDate,TimePickerView.Type type ){
        show(title,startDate,endDate,null,type);
    }

    public void show(String title, Calendar startDate, Calendar endDate,Calendar selectedDate ,TimePickerView.Type type){
        if(pvTime == null){
            //时间选择器
            TimePickerView.Builder builder = new TimePickerView.Builder(mContext, new TimePickerView.OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {//选中事件回调
                    if(timeCallBack != null){
                        timeCallBack.onTimeSelect(date,v);
                    }
                }
            })
                    .setLabel("年", "月", "日", "时", "分", "秒") //设置空字符串以隐藏单位提示   hide label
                    .setContentSize(15)
                    .setTitleSize(16)
                    .setDividerColor(Color.BLACK)
                    .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
//                    .setOutSideCancelable(false)// default is true
                    .setRangDate(startDate, endDate);

            if(selectedDate != null){
                builder.setDate(selectedDate);
            }else{
                builder.setDate(Calendar.getInstance());
            }

            if(type != null){
                builder.setType(type);
            }else{
                builder.setType(TimePickerView.Type.YEAR_MONTH_DAY);
            }
            if(title != null){
                builder.setTitleText(title);
            }
            pvTime  = builder.build();
        }

        if(!pvTime.isShowing()){
            pvTime.show();
        }
    }


    public interface  TimeCallBack{
        void onTimeSelect(Date date, View v);
    }
}
